//
//  ViewController.m
//  JSDropDownMenuDemo
//
//  Created by Jsfu on 15-1-12.
//  Copyright (c) 2015年 jsfu. All rights reserved.
//

#import "ZHPullDownMenuController.h"
#import "JSDropDownMenu.h"
#import "ZHHttpTool.h"
#import "NBTypeItem.h"
#import "MJExtension.h"
#import "NBTwoTypeItem.h"


@interface ZHPullDownMenuController ()<JSDropDownMenuDataSource,JSDropDownMenuDelegate>

@property (nonatomic,strong) NSArray *data;
@property (nonatomic,strong) NSMutableArray *data1;
@property (nonatomic,strong) NSMutableArray *data2;
@property (nonatomic,strong) NSMutableArray *data3;


@property (nonatomic,strong) NSArray *twodata1;
@property (nonatomic,strong) NSArray *twodata2;
@property (nonatomic,strong) NSArray *twodata3;
@property (nonatomic,strong) NSString *mallId;
@property (nonatomic,strong) NSString *tagId;
@property (nonatomic,strong) NSString *typeId;


@property (nonatomic,assign) NSInteger currentData1Index;
@property (nonatomic,assign) NSInteger currentData2Index;
@property (nonatomic,assign) NSInteger currentData3Index;
    
@property (nonatomic,assign) NSInteger currentData1SelectedIndex;
@property (nonatomic,strong)  JSDropDownMenu *menu;


@end

@implementation ZHPullDownMenuController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //0.告诉框架新值和旧值的对应关系
    [NBTwoTypeItem mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
        return @{
                 @"ID":@"id",
                 };
    }];

    
    //初始化下拉筛选菜单
    [self SetUpData];
    
    _data1=[NSMutableArray array];
    
    _data2=[NSMutableArray array];
    
    _data3=[NSMutableArray array];
    
}

//设置下拉筛选菜单初始化方法
-(void)SetUpData
{
    // 指定默认选中
    _currentData1Index = 0;
    _currentData1SelectedIndex = 1;
    
    _data1=[NSMutableArray arrayWithObjects:@"全部商城", nil];
    _data2=[NSMutableArray arrayWithObjects:@"全部热点", nil];
    _data3=[NSMutableArray arrayWithObjects:@"全部推荐", nil];
    
    [self GetType:@"ss"];
    
    _menu = [[JSDropDownMenu alloc] initWithOrigin:CGPointMake(0, 44) andHeight:45];
    _menu.indicatorColor = [UIColor colorWithRed:175.0f/255.0f green:175.0f/255.0f blue:175.0f/255.0f alpha:1.0];
    _menu.separatorColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0];
    _menu.textColor = [UIColor colorWithRed:83.f/255.0f green:83.f/255.0f blue:83.f/255.0f alpha:1.0f];
    _menu.dataSource = self;
    _menu.delegate = self;

    
    [self.view addSubview:_menu];

}
//请求数据
-(void)GetType:(NSString *)type
{
    
    
    NSString *url=[NSString stringWithFormat:@"http://mrobot.pconline.com.cn/s/best/faxian/screen.xsp?v=2.3.0&platform=android"];
    
    [ZHHttpTool get:url params:nil success:^(id responseObj) {
        
        self.data = [NBTypeItem mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
        
        NBTypeItem *item1=self.data[0];
        NBTypeItem *item2=self.data[1];
        NBTypeItem *item3=self.data[2];
        
        [_data1 addObject:@"全部商城"];
        [_data2 addObject:@"全部热点"];
        [_data3 addObject:@"全部推荐"];
        
        for (int i=0; i<item1.screens.count; i++) {
            
         _twodata1=[NBTwoTypeItem mj_objectArrayWithKeyValuesArray:item1.screens];
            
            NBTwoTypeItem *item=_twodata1[i];
            
            [_data1 addObject:item.name];
            
//            NSLog(@"%@",_data1);
        }
        
        for (int i=0; i<item2.screens.count; i++) {
            
            _twodata2=[NBTwoTypeItem mj_objectArrayWithKeyValuesArray:item2.screens];
            
            NBTwoTypeItem *item=_twodata2[i];
            
            [_data2 addObject:item.name];
            
            //            NSLog(@"%@",_data1);
        }
        
        for (int i=0; i<item3.screens.count; i++) {
            
            _twodata3=[NBTwoTypeItem mj_objectArrayWithKeyValuesArray:item3.screens];
            
            NBTwoTypeItem *item=_twodata3[i];
            
            [_data3 addObject:item.name];
            
            //            NSLog(@"%@",_data1);
        }


//
    } failure:^(NSError *error) {
        
        //打印错误信息
        NSLog(@"%@",error);
       
    }];
    
    
}


- (NSInteger)numberOfColumnsInMenu:(JSDropDownMenu *)menu {
    
    return 3;
}

-(BOOL)displayByCollectionViewInColumn:(NSInteger)column{
    
//    if (column==2) {
//        
//        return YES;
//    }
    //1
    return NO;
}

-(BOOL)haveRightTableViewInColumn:(NSInteger)column{
    
//    if (column==0) {
//        return YES;
//    }
    //2
    return NO;
}

-(CGFloat)widthRatioOfLeftColumn:(NSInteger)column{
    
//    if (column==0) {
//        return 0.3;
//    }
    //5
    return 1;
}

-(NSInteger)currentLeftSelectedRow:(NSInteger)column{
    //3
    if (column==0) {
        
        return _currentData1Index;
        
    }
    if (column==1) {
        
        return _currentData2Index;
    }
    if (column==2) {
        
        return _currentData3Index;
    }
    
    return 0;
}

- (NSInteger)menu:(JSDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow{
    //4
    if (column==0) {
        
        return _data1.count;
        
    } else if (column==1){
        
        return _data2.count;
        
    } else if (column==2){
        
        return _data3.count;
    }
    
    return 0;
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForColumn:(NSInteger)column{
    
    switch (column) {
//        case 0: return [[_data1[_currentData1Index] objectForKey:@"data"] objectAtIndex:_currentData1SelectedIndex];
        case 0: return _data1[_currentData1Index];

            break;
        case 1: return _data2[_currentData2Index];
            break;
        case 2: return _data3[_currentData3Index];
            break;
        default:
            return nil;
            break;
    }
     }

- (NSString *)menu:(JSDropDownMenu *)menu titleForRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column==0) {
        
        return _data1[indexPath.row];
        
    } else if (indexPath.column==1) {
        
        return _data2[indexPath.row];
        

        
    } else {
        
        return _data3[indexPath.row];
        
    }
   
    
}

- (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column == 0) {
        
      _currentData1Index = indexPath.row;
        
        //取出选中项的ID
        if (indexPath.row==0) {
            self.mallId=@"0";
            return;
        }
    NBTwoTypeItem *item=self.twodata1[indexPath.row-1];
        //保存ID到全局属性中
        self.mallId=item.ID;
        
    
    } else if(indexPath.column == 1){
        
        _currentData2Index = indexPath.row;
        //取出选中项的ID
        
        if (indexPath.row==0) {
           self.tagId=@"0";
            return;
        }
        NBTwoTypeItem *item=self.twodata2[indexPath.row-1];
        //保存ID到全局属性中
        self.tagId=item.ID;
       
        
    } else{
        
        _currentData3Index = indexPath.row;
        //取出选中项的ID
        if (indexPath.row==0) {
          self.typeId=@"0";
            return;
        }
        NBTwoTypeItem *item=self.twodata3[indexPath.row-1];
        //保存ID到全局属性中
        self.typeId=item.ID;
    }
    
}


@end
